/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.server.dac.dao;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import pos.client.dac.dao.TipoLinkU_PDAO;
import pos.domain.TipoLinkU_P;
import pos.server.dac.ConnectionManager;
import pos.server.dac.exceptions.EccezioneCRUD;

/**
 * @author Alessio
 */
public class TipoLinkU_PDAOImpl extends RemoteServiceServlet implements TipoLinkU_PDAO
{
    private final String INSERT_SQL = "INSERT INTO U_P (Utente, Piano) VALUES (?,?)";
    private final String DELETE_SQL = "DELETE FROM U_P WHERE Piano = ?";
    
    @Override
    public void inserisciDatisuDb(TipoLinkU_P obj) throws EccezioneCRUD
    {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_SQL);
            pstmt.setString(1, obj.getUtente_login());
            pstmt.setInt(2, obj.getPiano_id());
            pstmt.executeUpdate();
            pstmt.close();
            con.close();
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public void eliminaDatisuDb(TipoLinkU_P obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_SQL);
            pstmt.setInt(1, obj.getPiano_id());
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }
}
